
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using Apps.Models;
using Apps.Common;
using Microsoft.Practices.Unity;
using System.Transactions;
using Apps.BLL.Core;
using Apps.Locale;
using LinqToExcel;
using System.IO;
using System.Text;
using Apps.IDAL.JOB;
using Apps.Models.JOB;
using Apps.IBLL.JOB;
namespace Apps.BLL.JOB
{
	public partial class JOB_TASKJOBSBLL: Virtual_JOB_TASKJOBSBLL,IJOB_TASKJOBSBLL
	{
        

	}
	public class Virtual_JOB_TASKJOBSBLL
	{
        [Dependency]
        public IJOB_TASKJOBSRepository m_Rep { get; set; }

		public virtual List<JOB_TASKJOBSModel> GetList(ref GridPager pager, string queryStr)
        {

            IQueryable<JOB_TASKJOBS> queryData = null;
            if (!string.IsNullOrWhiteSpace(queryStr))
            {
                queryData = m_Rep.GetList(
								a=>a.sno.Contains(queryStr)
								|| a.taskName.Contains(queryStr)
								|| a.Id.Contains(queryStr)
								|| a.taskTitle.Contains(queryStr)
								|| a.taskCmd.Contains(queryStr)
								
								
								|| a.creator.Contains(queryStr)
								|| a.procName.Contains(queryStr)
								|| a.procParams.Contains(queryStr)
								);
            }
            else
            {
                queryData = m_Rep.GetList();
            }
            pager.totalRows = queryData.Count();
            //排序
            queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
            return CreateModelList(ref queryData);
        }

		public virtual List<JOB_TASKJOBSModel> GetListByParentId(ref GridPager pager, string queryStr,object parentId)
        {
			return new List<JOB_TASKJOBSModel>();
		}

        public virtual List<JOB_TASKJOBSModel> CreateModelList(ref IQueryable<JOB_TASKJOBS> queryData)
        {

            List<JOB_TASKJOBSModel> modelList = (from r in queryData
                                              select new JOB_TASKJOBSModel
                                              {
													sno = r.sno,
													taskName = r.taskName,
													Id = r.Id,
													taskTitle = r.taskTitle,
													taskCmd = r.taskCmd,
													crtDt = r.crtDt,
													state = r.state,
													creator = r.creator,
													procName = r.procName,
													procParams = r.procParams,
          
                                              }).ToList();

            return modelList;
        }

        public virtual bool Create(ref ValidationErrors errors, JOB_TASKJOBSModel model)
        {
            try
            {
                JOB_TASKJOBS entity = m_Rep.GetById(model.sno);
                if (entity != null)
                {
                    errors.Add(Resource.PrimaryRepeat);
                    return false;
                }
                entity = new JOB_TASKJOBS();
               				entity.sno = model.sno;
				entity.taskName = model.taskName;
				entity.Id = model.Id;
				entity.taskTitle = model.taskTitle;
				entity.taskCmd = model.taskCmd;
				entity.crtDt = model.crtDt;
				entity.state = model.state;
				entity.creator = model.creator;
				entity.procName = model.procName;
				entity.procParams = model.procParams;
  

                if (m_Rep.Create(entity))
                {
                    return true;
                }
                else
                {
                    errors.Add(Resource.InsertFail);
                    return false;
                }
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }



         public virtual bool Delete(ref ValidationErrors errors, object id)
        {
            try
            {
                if (m_Rep.Delete(id) == 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

        public virtual bool Delete(ref ValidationErrors errors, object[] deleteCollection)
        {
            try
            {
                if (deleteCollection != null)
                {
                    using (TransactionScope transactionScope = new TransactionScope())
                    {
                        if (m_Rep.Delete(deleteCollection) == deleteCollection.Length)
                        {
                            transactionScope.Complete();
                            return true;
                        }
                        else
                        {
                            Transaction.Current.Rollback();
                            return false;
                        }
                    }
                }
                return false;
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

		
       

        public virtual bool Edit(ref ValidationErrors errors, JOB_TASKJOBSModel model)
        {
            try
            {
                JOB_TASKJOBS entity = m_Rep.GetById(model.sno);
                if (entity == null)
                {
                    errors.Add(Resource.Disable);
                    return false;
                }
                              				entity.sno = model.sno;
				entity.taskName = model.taskName;
				entity.Id = model.Id;
				entity.taskTitle = model.taskTitle;
				entity.taskCmd = model.taskCmd;
				entity.crtDt = model.crtDt;
				entity.state = model.state;
				entity.creator = model.creator;
				entity.procName = model.procName;
				entity.procParams = model.procParams;
 


                if (m_Rep.Edit(entity))
                {
                    return true;
                }
                else
                {
                    errors.Add(Resource.NoDataChange);
                    return false;
                }

            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

      

        public virtual JOB_TASKJOBSModel GetById(object id)
        {
            if (IsExists(id))
            {
                JOB_TASKJOBS entity = m_Rep.GetById(id);
                JOB_TASKJOBSModel model = new JOB_TASKJOBSModel();
                              				model.sno = entity.sno;
				model.taskName = entity.taskName;
				model.Id = entity.Id;
				model.taskTitle = entity.taskTitle;
				model.taskCmd = entity.taskCmd;
				model.crtDt = entity.crtDt;
				model.state = entity.state;
				model.creator = entity.creator;
				model.procName = entity.procName;
				model.procParams = entity.procParams;
 
                return model;
            }
            else
            {
                return null;
            }
        }


		 /// <summary>
        /// 校验Excel数据,这个方法一般用于重写校验逻辑
        /// </summary>
        public virtual bool CheckImportData(string fileName, List<JOB_TASKJOBSModel> list,ref ValidationErrors errors )
        {
          
            var targetFile = new FileInfo(fileName);

            if (!targetFile.Exists)
            {

                errors.Add("导入的数据文件不存在");
                return false;
            }

            var excelFile = new ExcelQueryFactory(fileName);

            //对应列头
			 				 excelFile.AddMapping<JOB_TASKJOBSModel>(x => x.sno, "sno");
				 excelFile.AddMapping<JOB_TASKJOBSModel>(x => x.taskName, "taskName");
				 excelFile.AddMapping<JOB_TASKJOBSModel>(x => x.taskTitle, "taskTitle");
				 excelFile.AddMapping<JOB_TASKJOBSModel>(x => x.taskCmd, "taskCmd");
				 excelFile.AddMapping<JOB_TASKJOBSModel>(x => x.crtDt, "crtDt");
				 excelFile.AddMapping<JOB_TASKJOBSModel>(x => x.state, "state");
				 excelFile.AddMapping<JOB_TASKJOBSModel>(x => x.creator, "creator");
				 excelFile.AddMapping<JOB_TASKJOBSModel>(x => x.procName, "procName");
				 excelFile.AddMapping<JOB_TASKJOBSModel>(x => x.procParams, "procParams");
 
            //SheetName
            var excelContent = excelFile.Worksheet<JOB_TASKJOBSModel>(0);
            int rowIndex = 1;
            //检查数据正确性
            foreach (var row in excelContent)
            {
                var errorMessage = new StringBuilder();
                var entity = new JOB_TASKJOBSModel();
						 				  entity.sno = row.sno;
				  entity.taskName = row.taskName;
				  entity.Id = row.Id;
				  entity.taskTitle = row.taskTitle;
				  entity.taskCmd = row.taskCmd;
				  entity.crtDt = row.crtDt;
				  entity.state = row.state;
				  entity.creator = row.creator;
				  entity.procName = row.procName;
				  entity.procParams = row.procParams;
 
                //=============================================================================
                if (errorMessage.Length > 0)
                {
                    errors.Add(string.Format(
                        "第 {0} 列发现错误：{1}{2}",
                        rowIndex,
                        errorMessage,
                        "<br/>"));
                }
                list.Add(entity);
                rowIndex += 1;
            }
            if (errors.Count > 0)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        public virtual void SaveImportData(IEnumerable<JOB_TASKJOBSModel> list)
        {
            try
            {
                using (DBContainer db = new DBContainer())
                {
                    foreach (var model in list)
                    {
                        JOB_TASKJOBS entity = new JOB_TASKJOBS();
                       						entity.sno = model.sno;
						entity.taskName = model.taskName;
						entity.Id = ResultHelper.NewId;
						entity.taskTitle = model.taskTitle;
						entity.taskCmd = model.taskCmd;
						entity.crtDt = model.crtDt;
						entity.state = model.state;
						entity.creator = model.creator;
						entity.procName = model.procName;
						entity.procParams = model.procParams;
 
                        db.JOB_TASKJOBS.Add(entity);
                    }
                    db.SaveChanges();
                }
            }
            catch
            {
                throw;
            }
        }


        public virtual bool IsExists(object id)
        {
            return m_Rep.IsExist(id);
        }
		  public void Dispose()
        { 
            
        }

	}
}
